package drds.server.sqlparser.sqlobject.statement.query.order_by;

import java.util.ArrayList;
import java.util.List;

import drds.server.sqlparser.sqlobject.AbstractSqlObject;
import drds.server.sqlparser.sqlobject.expression.Expression;
import drds.server.sqlparser.visitor.Visitor;

/**
 * 
 * @author 曹正辉<330937205@qq.com>
 * 
 */
public class OrderBy extends AbstractSqlObject {

	protected final List<OrderByItem> orderByItemList = new ArrayList<OrderByItem>();

	public OrderBy() {

	}

	public OrderBy(Expression expression) {
		OrderByItem orderByItem = new OrderByItem(expression);
		addOrderByItem(orderByItem);
	}

	public void addOrderByItem(OrderByItem orderByItem) {
		if (orderByItem != null) {
			orderByItem.setParent(this);
		}
		this.orderByItemList.add(orderByItem);
	}

	public List<OrderByItem> getOrderByItemList() {
		return this.orderByItemList;
	}

	protected void accept0(Visitor visitor) {
		if (visitor.visit(this)) {
			acceptChild(visitor, this.orderByItemList);
		}

		visitor.endVisit(this);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((orderByItemList == null) ? 0 : orderByItemList.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null)
			return false;
		if (getClass() != o.getClass())
			return false;
		OrderBy other = (OrderBy) o;
		if (orderByItemList == null) {
			if (other.orderByItemList != null)
				return false;
		} else if (!orderByItemList.equals(other.orderByItemList))
			return false;
		return true;
	}
}
